package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

class PermutationInString {
    // 2022-11-08 15:23:15
    public static void main(String[] args) {
        Solution solution = new PermutationInString().new Solution();
        solution.checkInclusion("ab", "eidbaooo");
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean checkInclusion(String s1, String s2) {
            Map<Character, Integer> map = new HashMap<>();

            Map<Character, Integer> window = new HashMap<>();
            for (int i = 0; i < s1.length(); i++) {
                map.put(s1.charAt(i), map.getOrDefault(s1.charAt(i), 0) + 1);
            }
            int left = 0;
            int right = 0;
            while (right < s2.length()) {
                window.put(s2.charAt(right), window.getOrDefault(s2.charAt(right), 0) + 1);
                if (map.equals(window)) {
                    return true;
                }
                right++;
                if ((right - left + 1) > s1.length()) {
                    if (window.get(s2.charAt(left)) == 1) {
                        window.remove(s2.charAt(left));
                    } else {
                        window.put(s2.charAt(left), window.get(s2.charAt(left)) - 1);
                    }
                    left++;
                }

            }

            return false;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}